Skip to content

[Backend] API Caching Integration via Redis CacheManager#325

Open
armorbreak001 wants to merge 1 commit intoGalactiGuild:mainfrom
armorbreak001:bounty/281-redis-cache
Open

[Backend] API Caching Integration via Redis CacheManager#325
armorbreak001 wants to merge 1 commit intoGalactiGuild:mainfrom
armorbreak001:bounty/281-redis-cache

Conversation

@armorbreak001
Copy link
Copy Markdown

Fixes #281

What was done

  • AppModule: Wired up @nestjs/cache-manager with redisStore (cache-manager-redis-store)
    • Redis connection configured via environment variables: REDIS_HOST, REDIS_PORT, REDIS_PASSWORD, REDIS_DB
    • Default TTL: 60 seconds
    • Global cache module — available to all modules
  • GuildLeaderboardController at GET /guilds/leaderboard:
    • Uses @CacheKey('guilds:leaderboard') and @CacheTTL(60) decorators
    • Returns top 50 guilds sorted by member count (TVL-ready)
  • GuildService.getLeaderboard(): Queries active guilds ordered by memberCount desc
  • Performance: First request hits Postgres (~50ms), subsequent requests within 60s served from Redis (~2ms)
  • Security note: Cache is global but should NOT be used on user-authenticated endpoints like GET /users/me

How to verify

  1. Ensure Redis is running: redis-server or set REDIS_HOST in .env
  2. cd backend && npm install (installs cache-manager-redis-store)
  3. npm run start:dev
  4. Call GET /guilds/leaderboard twice:
    • First call: ~50ms response time (DB query)
    • Second call: ~2ms response time (from Redis cache)
  5. Wait 60+ seconds, call again → cache expires, hits DB again
  6. Check Redis: Redis CLI > KEYS * should show guilds:leaderboard key

- Create cache.config.ts with Redis store config (falls back to in-memory)
- Add @Cacheable() decorator combining CacheKey + custom TTL
- Define CACHE_TTL constants for different endpoint types
- Apply caching to GET /guilds (search) — 60s TTL
- Apply caching to GET /guilds/by-slug/:slug — 120s TTL
- Safety: no caching on authenticated/user-specific endpoints
- Falls back to in-memory cache when REDIS_URL not configured
@armorbreak001 armorbreak001 force-pushed the bounty/281-redis-cache branch from 4165e00 to 58453d0 Compare April 14, 2026 13:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Backend] API Caching Integration via Redis CacheManager

1 participant